package oj.oj_144;
/**
 * 二叉树的前序遍历
 * https://leetcode.cn/problems/binary-tree-preorder-traversal/
 */

import java.util.*;

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {

        List<Integer> ans = new ArrayList<>();

        if (root == null) {
            return ans;
        }

        int rootValue = root.val;
        ans.add(rootValue);
        ans.addAll(preorderTraversal(root.left));
        ans.addAll(preorderTraversal(root.right));
        return ans;
    }
    public static void main(String[] args) {
        TreeNode n1 = new TreeNode(1);  // A
        TreeNode n2 = new TreeNode(2);  // B
        TreeNode n3 = new TreeNode(3);  // C
        TreeNode n4 = new TreeNode(4);  // D
        TreeNode n5 = new TreeNode(5);  // E
        TreeNode n6 = new TreeNode(6);  // F
        TreeNode n7 = new TreeNode(7);  // G
        TreeNode n8 = new TreeNode(8);  // H
        TreeNode n9 = new TreeNode(9);  // I
        TreeNode n10 = new TreeNode(10);    // J
        TreeNode n11 = new TreeNode(11);    // K

        n1.left = n2;   n1.right = n3;
        n2.left = n4;   n2.right = n5;
        n3.left = null; n3.right = n6;
        n4.left = null; n4.right = null;
        n5.left = n7;   n5.right = n8;
        n6.left = n9;   n6.right = null;
        n9.left = n10;  n9.right = n11;

        Solution solution = new Solution();
        List<Integer> list = solution.preorderTraversal(n1);
        System.out.println(list);
    }
}
